package com.itheima.mall.service.smsService;

import cn.hutool.core.collection.CollUtil;
import com.itheima.mall.commons.R;
import com.itheima.pojo.sms.SmsFlashPromotionProductRelation;
import com.itheima.pojo.sms.SmsFlashPromotionSession;
import com.itheima.vo.sms.SmsFlashPromotionSessionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SmsFlashPromotionSessionService {
    @DubboReference
    private SmsFlashPromotionSessionServiceApi smsFlashPromotionSessionServiceApi;
    @DubboReference
    private SmsFlashPromotionProductRelationServiceApi smsFlashPromotionProductRelationServiceApi;
    /**
     * 添加场次
     */
    public R create(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionServiceApi.create(smsFlashPromotionSession);
        return R.ok("操作成功!");
    }
    /**
     * 删除场次
     */
    public R delete(Long id) {
        smsFlashPromotionSessionServiceApi.delete(id);
        return R.ok("操作成功!");
    }
    /**
     * 获取全部场次
     */
    public R list() {
        List<SmsFlashPromotionSession> smsFlashPromotionSessions =smsFlashPromotionSessionServiceApi.list();
        return R.ok(smsFlashPromotionSessions);
    }

    /**
     * 获取全部可选场次及其数量
     */
    public R selectList(Long flashPromotionId) {
        List<SmsFlashPromotionProductRelation> list = smsFlashPromotionProductRelationServiceApi.selectList(flashPromotionId);
        if (CollUtil.isEmpty(list)) {
            return R.ok(null);
        }
        //获取sessionid
        List<Long> flashPromotionSessionIds = CollUtil.getFieldValues(list, "flashPromotionSessionId", Long.class);
        List<SmsFlashPromotionSession> smsFlashPromotionSessions =smsFlashPromotionSessionServiceApi.selectByIds(flashPromotionSessionIds);
        List<SmsFlashPromotionSessionVo> result=new ArrayList<>();
        for (SmsFlashPromotionSession smsFlashPromotionSession : smsFlashPromotionSessions) {
            SmsFlashPromotionSessionVo smsFlashPromotionSessionVo=new SmsFlashPromotionSessionVo();
            BeanUtils.copyProperties(smsFlashPromotionSession, smsFlashPromotionSessionVo);
            smsFlashPromotionSessionVo.setProductCount(smsFlashPromotionProductRelationServiceApi.getProductCount(flashPromotionId,smsFlashPromotionSession.getId()));
            result.add(smsFlashPromotionSessionVo);
        }
        return R.ok(result);
    }

    /**
     * 修改启用状态
     */
    public R updateStatus(Map<Long, Long> map) {
        Long id = map.get("id");
        Long status = map.get("status");
        smsFlashPromotionSessionServiceApi.updateStatus(id,status);
        return R.ok("操作成功");
    }

    /**
     * 修改场次
     */
    public R update(Long id, SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionServiceApi.update(id,smsFlashPromotionSession);
        return R.ok("操作成功!");
    }

    /**
     * 获取场次详情
     */
    public R getFlashSession(Long id) {
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionServiceApi.getFlashSession(id);
        return R.ok(smsFlashPromotionSession);
    }
}
